using System;
using System.Collections.Generic;

using Atomic.Libraries.Mathematics;

namespace Atomic.Libraries
{
	[Serializable]
	public class LinkedVector<T>
	{
		private Vector vector;
		private Dictionary<T, int> indices;

		public LinkedVector(IEnumerable<T> parent, IDictionary<T, double> values)
			: this(new List<T>(parent), values)
		{
		}

		private LinkedVector(IList<T> parent, IDictionary<T, double> values)
		{
			int n = parent.Count;
			double[] entries = new double[n];

			indices = new Dictionary<T, int>();
			for (int i = 0; i < n; i++)
			{
				T key = parent[i];
				indices.Add(key, i);
				if (values.ContainsKey(key))
				{
					entries[i] = values[key];
				}
			}

			vector = new Vector(entries);
		}

		public Vector ToVector()
		{
			return vector;
		}

		public double this[T key]
		{
			get
			{
				return this[indices[key]];
			}
		}

		public double this[int index]
		{
			get
			{
				return vector[index];
			}
		}

		public int Length
		{
			get
			{
				return vector.Length;
			}
		}
	}
}
